package com.lifesting.front;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import com.sun.faces.renderkit.html_basic.HtmlBasicRenderer;

public class FilterRender extends HtmlBasicRenderer {
	public static final String RENDER_TYPE = "com.lifesting.render.filter";
	public static final String NO_FILTER = "-12345";

	@Override
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		super.encodeBegin(context, component);
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement("p", component);
		String filter_id = component.getClientId(context);
		Filter filter = (Filter) component;
		writer.write(filter.getText() + " : ");
		writer.startElement("select", component);
		writer.writeAttribute("id", filter_id + ".1", null);
		writer.writeAttribute("name", filter_id + ".1", null);
		Table table = (Table) filter.getParent().findComponent(
				filter.getTableFor());
		List<Column> columns = FacesUtil.getColumn(table);
		// 默认一个空，值为-100000，不可能会是bean filed name
		writer.startElement("option", component);
		writer.writeAttribute("value", NO_FILTER, null);
		writer.writeText("--NO Filter--", null);
		writer.endElement("option");
		for (int i = 0; i < columns.size(); i++) {
			Column column = columns.get(i);
			if (column.getEnableFilter()) {
				writer.startElement("option", component);
				writer.writeAttribute("value", column.getField(), null);
				if (column.getField().equals(table.getFilterField())) {
					writer.writeAttribute("selected", "selected", null);
				}
				writer.writeText(column.getText(), null);
				writer.endElement("option");
			}
		}
		writer.endElement("select");
		writer.write(" ");
		writer.startElement("input", component);
		writer.writeAttribute("type", "input", null);
		writer.writeAttribute("size", "20", null);
		writer.writeAttribute("name", filter_id + ".2", null);
		writer.writeAttribute("id", filter_id + ".2", null);
		writer.writeAttribute("value", table.getFilterLike(), null);
		writer.endElement("input");
		writer.startElement("input", component);
		writer.writeAttribute("type", "submit", null);
		writer.writeAttribute("name", filter_id + ".3", null);
		writer.writeAttribute("id", filter_id + ".3", null);
		writer.writeAttribute("value", "确定", null);

		writer.endElement("input");

	}

	@Override
	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		writer.endElement("p");
	}

	@Override
	public void decode(FacesContext context, UIComponent component) {
		Map<String, String> req = context.getExternalContext()
				.getRequestParameterMap();
		assert (component instanceof Filter);
		Filter filter = (Filter) component;
		String id = filter.getClientId(context);
		Table table = (Table) filter.getParent().findComponent(
				filter.getTableFor());
		if (req.containsKey(id + ".3")) {
			String ff = req.get(id + ".1");
			String fl = req.get(id + ".2");
			if (NO_FILTER.equals(ff)) { //只有设置了Filter才发通知，否则让table恢复filter为null
				ff = null; fl = null; //clear filter
			}
			FilterEvent evt = new FilterEvent(table, ff, fl);
			table.queueEvent(evt);
		}
	}
}
